export function enumVal2str<T extends Record<string, unknown>>(
  enumObj: T,
  value: unknown
): string | undefined {
  if (typeof value === 'number') {
    return enumObj[value] as string | undefined;
  }
  for (const key in enumObj) {
    if (enumObj[key] === value) {
      return key;
    }
  }
  return undefined;
}
export function enum2Collection<T extends Record<string, any>>(
  enumObj: T
): Array<{ key: number | string; value: string }> {
  const result: Array<{ key: number | string; value: string }> = [];

  for (const key in enumObj) {
    if (isNaN(Number(key))) {
      const enumValue = enumObj[key];
      result.push({
        key: enumValue,
        value: key
      });
    }
  }
  return result;
}
export function dateFormat(d: Date) {
  return d.toLocaleDateString('sv-SE'); // 瑞典格式就是 yyyy-mm-dd
}
export function dateTimeFormat(date: Date) {
  const d = new Date(date);
  const year = d.getFullYear();
  const month = String(d.getMonth() + 1).padStart(2, '0');
  const day = String(d.getDate()).padStart(2, '0');
  const hours = String(d.getHours()).padStart(2, '0');
  const minutes = String(d.getMinutes()).padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}`;
}
export function timeFormat(date: Date) {
  const d = new Date(date);
  const year = d.getFullYear();
  const month = String(d.getMonth() + 1).padStart(2, '0');
  const day = String(d.getDate()).padStart(2, '0');
  const hours = String(d.getHours()).padStart(2, '0');
  const minutes = String(d.getMinutes()).padStart(2, '0');
  return `${hours}:${minutes}`;
}

export const genRandomStr = (num: number) => {
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';
  const charactersLength = characters.length;
  for (let i = 0; i < num; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
}